package com.jlb.demo.entity;

/**
 * 商品实体类 - 用于课后练习
 * 继承BaseEntity，获得id、createTime、updateTime字段
 */
public class Product extends BaseEntity {
    
    /**
     * 商品名称
     */
    private String name;
    
    /**
     * 商品价格
     */
    private Double price;
    
    /**
     * 库存数量
     */
    private Integer stock;
    
    /**
     * 商品描述
     */
    private String description;
    
    /**
     * 商品分类
     */
    private String category;
    
    /**
     * 默认构造方法
     */
    public Product() {
        super();
        this.stock = 0;
    }
    
    /**
     * 带参数的构造方法
     */
    public Product(Long id, String name, Double price, Integer stock) {
        super(id);
        this.name = name;
        this.price = price;
        this.stock = stock;
    }
    
    /**
     * 减少库存 - 演示业务逻辑
     * @param quantity 减少的数量
     * @throws IllegalArgumentException 当数量不合法时抛出异常
     * @throws IllegalStateException 当库存不足时抛出异常
     */
    public void reduceStock(Integer quantity) {
        if (quantity == null || quantity <= 0) {
            throw new IllegalArgumentException("减少的库存数量必须大于0");
        }
        
        if (quantity > this.stock) {
            throw new IllegalStateException(
                String.format("库存不足，当前库存：%d，需要：%d", this.stock, quantity)
            );
        }
        
        this.stock -= quantity;
        setUpdateTime();
        System.out.println(String.format("商品 %s 库存减少 %d，剩余库存：%d", name, quantity, stock));
    }
    
    /**
     * 增加库存
     * @param quantity 增加的数量
     */
    public void increaseStock(Integer quantity) {
        if (quantity == null || quantity <= 0) {
            throw new IllegalArgumentException("增加的库存数量必须大于0");
        }
        
        this.stock += quantity;
        setUpdateTime();
        System.out.println(String.format("商品 %s 库存增加 %d，当前库存：%d", name, quantity, stock));
    }
    
    /**
     * 检查库存是否充足
     * @param requiredQuantity 需要的数量
     * @return 是否充足
     */
    public boolean hasEnoughStock(Integer requiredQuantity) {
        return stock >= requiredQuantity;
    }
    
    /**
     * 检查是否库存不足（少于指定数量）
     * @param threshold 阈值
     * @return 是否库存不足
     */
    public boolean isLowStock(Integer threshold) {
        return stock < threshold;
    }
    
    /**
     * 获取商品摘要
     * @return 商品摘要
     */
    public String getSummary() {
        return String.format("商品[ID=%d, 名称=%s, 价格=%.2f, 库存=%d, 分类=%s]", 
                getId(), name, price, stock, category);
    }
    
    // Getter和Setter方法
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public Double getPrice() {
        return price;
    }
    
    public void setPrice(Double price) {
        this.price = price;
    }
    
    public Integer getStock() {
        return stock;
    }
    
    public void setStock(Integer stock) {
        this.stock = stock;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getCategory() {
        return category;
    }
    
    public void setCategory(String category) {
        this.category = category;
    }
    
    @Override
    public String toString() {
        return "Product{" +
                "id=" + getId() +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", stock=" + stock +
                ", description='" + description + '\'' +
                ", category='" + category + '\'' +
                ", createTime=" + getCreateTime() +
                ", updateTime=" + getUpdateTime() +
                '}';
    }
} 